package com.oocl.servlet;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.oocl.log4j.Log4j;
import com.oocl.util.JsonUtil;
import com.oocl.vo.MerchandiseVO;

/**
 * Servlet implementation class AddCartServlet
 */
public class AddFoodToCartServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AddFoodToCartServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 *url:"http://127.0.0.1:9000/clientSys/addCart?name="+name+"&price="+price+"&merchantName="+merchantName+"&food_count="+food_count,

	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		
		HttpSession session = request.getSession();
		String json = request.getParameter("newItemJson");
		
		Log4j.getLogger().info("从前面获取到的信息为{}", java.net.URLDecoder.decode(json,"utf-8"));
		
		
		MerchandiseVO merchandiseVO = (MerchandiseVO) JsonUtil.parserToDTO(java.net.URLDecoder.decode(json,"utf-8"), MerchandiseVO.class);
		Log4j.getLogger().info("foodVO 转换为 json 后： {}", merchandiseVO.toString());
		
		
		
		Map<String, MerchandiseVO> shoppingCart = (HashMap<String, MerchandiseVO>) session.getAttribute("shoppingCart");
		
		String name = merchandiseVO.getName();
		int count = merchandiseVO.getCount();
		double price = merchandiseVO.getPrice();
		String merchantName = merchandiseVO.getMerchantName();
		String merchandiseId = merchandiseVO.getMerchandiseId();
		String img = merchandiseVO.getImg();
		
		if(shoppingCart != null && shoppingCart.containsKey(name)){
			Log4j.getLogger().info("购物车原来已经  存在食品{}", name);
			int current_count = shoppingCart.get(name).getCount()+ count;
			double totalMoney = current_count * price;
			merchandiseVO = new MerchandiseVO(merchandiseId, current_count, name, price, img, merchantName, totalMoney);
			shoppingCart.put(merchandiseId, merchandiseVO);
			session.setAttribute("shoppingCart", shoppingCart);
			
		}else if(shoppingCart != null && !shoppingCart.containsKey(name)){
			double totalMoney = count * price;
			merchandiseVO = new MerchandiseVO(merchandiseId, count, name, price, img, merchantName, totalMoney);
			shoppingCart.put(merchandiseId, merchandiseVO);
			session.setAttribute("shoppingCart", shoppingCart);
			
		}else{
			Log4j.getLogger().info("购物车原来 不  存在食品{}", name);
			Map<String, MerchandiseVO> newShoppingCart = new HashMap<String, MerchandiseVO>();
			double totalMoney = count * price;
			merchandiseVO = new MerchandiseVO(merchandiseId, count, name, price, img, merchantName, totalMoney);
			newShoppingCart.put(merchandiseId, merchandiseVO);
			session.setAttribute("shoppingCart", newShoppingCart);
			System.out.println("判断session有没有222： "+session.getAttribute("shoppingCart"));
		}
				
		Log4j.getLogger().info("准备发送到客户端的信息为： {}", JsonUtil.parserMerchandiseVOToString((Map<String, MerchandiseVO>) session.getAttribute("shoppingCart")));
		response.getWriter().write(JsonUtil.parserMerchandiseVOToString((Map<String, MerchandiseVO>) session.getAttribute("shoppingCart")));
//		MerchandiseVO merchandiseVO = JsonUtil2.parserToDTOs(json)
		
	}

}
